package cc.rengu.redp.base.runner;

import cc.rengu.redp.bizimpl.manager.UpmpUserManager;
import cc.rengu.redp.bizimpl.upmp.domain.UpmpUserInfo;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpCacheService;
import cc.rengu.redp.bizimpl.upmp.service.IUpmpUserInfoService;
import cc.rengu.redp.bizimpl.utils.RbdpClient;
import cc.rengu.redp.common.exception.RedisConnectException;
import cc.rengu.redp.common.render.dict.cache.DictCacheService;
import cc.rengu.redp.base.service.CacheService;
import cc.rengu.redp.system.domain.User;
import cc.rengu.redp.system.manager.UserManager;
import cc.rengu.redp.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 缓存初始化
 */
@Slf4j
@Component
public class CacheInitRunner implements ApplicationRunner {

    @Autowired
//    private UserService upmpUserInfoService;
    private IUpmpUserInfoService upmpUserInfoService;

    @Autowired
//    private CacheService upmpCacheService;
    private IUpmpCacheService upmpCacheService;

    @Autowired
    private DictCacheService dictCacheService;

    @Autowired
//    private UserManager userManager;
    private UpmpUserManager userManager;

    @Autowired
    private ConfigurableApplicationContext context;

    @Override
    public void run(ApplicationArguments args) {
        try {
//            List<String> ret = RbdpClient.getIntance1().getAllTasks();
//            log.info("批量 getAllTasks ······{}",ret);
            log.info("Redis连接中 ······");
            upmpCacheService.testConnect();

            log.info("缓存初始化 ······");
            log.info("缓存用户数据 ······");
            /*List<UpmpUserInfo> list = this.upmpUserInfoService.list();
            for (UpmpUserInfo user : list) {
                userManager.loadUserRedisCache(user);
            }*/

            log.info("缓存数据字典数据 ······");
            dictCacheService.initDictToCache();
        } catch (Exception e) {
            log.error("缓存初始化失败", e);
            log.error(" ____   __    _   _ ");
            log.error("| |_   / /\\  | | | |");
            log.error("|_|   /_/--\\ |_| |_|__");
            log.error("                        ");
            log.error("系统启动失败              ");
            if (e instanceof RedisConnectException) {
                log.error("Redis连接异常，请检查Redis连接配置并确保Redis服务已启动");
            }
            // 关闭系统
            context.close();
        }
    }
}
